package com.yunupmall.service.ford.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.CommodityVo;
import com.yunupmall.api.fordentity.Commodity;
import com.yunupmall.api.fordentity.Shoping;
import com.yunupmall.api.fordentity.SpecsCommodity;
import com.yunupmall.api.po.ShopingUpDataPo;
import com.yunupmall.api.po.SteamShopingDataPO;
import com.yunupmall.entity.ford.FordShopingMupData;
import com.yunupmall.mapper.FordMapper.ShopingMapper;
import com.yunupmall.service.admin.service.CommodityService;
import com.yunupmall.service.admin.service.SpecsCommdityService;
import com.yunupmall.service.ford.service.ShopingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class Shopingimpl extends ServiceImpl<ShopingMapper, Shoping> implements ShopingService {
    @Autowired
    private ShopingMapper fordShopingMap;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private SpecsCommdityService specCommdityService;

    @Override
    @Transactional
    public Integer insertShoppingData(Integer specsId, Integer counts, Integer mId) {
       if (ObjectUtil.isEmpty(specsId)) {
            throw new RuntimeException("商品规格ID为空");
       }
        if (ObjectUtil.isEmpty(counts)) {
            throw new RuntimeException("数量为空");
        }
        if (ObjectUtil.isEmpty(mId)) {
            throw new RuntimeException("用户ID为空");
        }
        Shoping shoping = new Shoping();
        shoping.setSpId(specsId);
        shoping.setCounts(counts);
        shoping.setMId(mId);
        shoping.setCreateTime(LocalDateTime.now());
        shoping.setDeflag(0);
        if (this.save(shoping)) {
         return shoping.getId();
        }else {
            throw new RuntimeException("新增失败");
        }
    }

    @Override
    public List<FordShopingMupData> queryshopData(Integer mId) {
        return fordShopingMap.queryshopData(mId);
    }

    @Override
    @Transactional
    public String deleteshopingID(Integer id) {
        if (ObjectUtil.isEmpty(id)){
            throw new RuntimeException("ID为空");
        }
        Shoping shoping = new Shoping();
        shoping.setId(id);
        shoping.setDeflag(1);
        if (this.updateById(shoping)){
            return "删除成功";
        }else {
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 获取购物车提交的订单信息
     * @param dataPoList
     * @return
     */
    @Override
    public List<SteamShopingDataPO> combinationShopingData(List<ShopingUpDataPo> dataPoList) {
        if (ObjectUtil.isEmpty(dataPoList)){
            throw new RuntimeException("参数为空");
        }
        List<SteamShopingDataPO> list = new ArrayList<>();
        final SteamShopingDataPO[] steamShopingDataPO = new SteamShopingDataPO[1];
        dataPoList.forEach(shopingUpDataPo -> {
            steamShopingDataPO[0] =new SteamShopingDataPO();
            CommodityVo commodityVo = new CommodityVo();
            commodityVo.setId(shopingUpDataPo.getCid());
            List<Commodity> commodities = commodityService.newcommotityID(commodityVo);
            if (commodities.size() > 0) {
              steamShopingDataPO[0].setCId(commodities.get(0).getId());
              steamShopingDataPO[0].setCName(commodities.get(0).getName());
              steamShopingDataPO[0].setImage(commodities.get(0).getExampleImage1());
              steamShopingDataPO[0].setCount(shopingUpDataPo.getCounts());
            }
            SpecsCommodity byid = specCommdityService.byid(shopingUpDataPo.getSpId());
            if (ObjectUtil.isNotEmpty(byid)){
                steamShopingDataPO[0].setCSku(byid.getCSku());
                steamShopingDataPO[0].setPrice(byid.getPrices());
                steamShopingDataPO[0].setSpeceId(byid.getId());
            }
            steamShopingDataPO[0].setShopingId(shopingUpDataPo.getShopingId());
            list.add(steamShopingDataPO[0]);
        });
        return list;
    }
}
